<?php
class view extends lib_base_base {
	static public $name=array();
	
	static public $value;
	
	static protected  $title;
	
	static protected $html;
	
	static protected $thme='theme';
	
	static protected $do;
	
	static protected $cn;
	
	static protected $config;
		
	/**
	 * 讀顯示層值
	 *
	 * @param string $string
	 * @return null
	 */
	public static function get($string){
		if(key_exists($string,self::$name)){
			return self::$name[$string];
		}else{
			return null;
		}
	}
	
	/**
	 * 顯示層傳值
	 *
	 * @param unknown_type $name
	 * @param unknown_type $value
	 */
	public static function set($name,$value){
		self::$name[$name]=$value;
	}
	
	public static function setTitle($title){
		self::$title=$title;
	}
	
	
	public static function setLayle()
	{
		
	}

	/**
	 * 設置顯示層頁面
	 *
	 * @param string $tpl
	 */
	public static  function tpl($tpl){
	    self::$config=R::getClass('config');
	    $config=self::$config;
	    $url=R::exurl();
	    self::$do=$url[0];
	    self::$cn=$url[1];
		$do=self::$do;
		$cn=self::$cn;	    
			  
        self::swiTpl($tpl);
        foreach (self::$name as $k=>$val){
            $$k=$val;
         }
        $title=self::$title;
        
		        
        $THEME=HZHIHU_ROOT.VIEW_FILE.self::$thme.HZHIHU_DIREND.$config->theme.HZHIHU_DIREND.'template'.HZHIHU_DIREND;
        if(!file_exists($THEME.$tpl)){
        	if(file_exists(HZHIHU_ROOT.VIEW_FILE.self::$thme.HZHIHU_DIREND.
        					$config->theme.HZHIHU_DIREND.'template'.HZHIHU_DIREND.self::$do.HZHIHU_DIREND.$tpl)){
        		include(HZHIHU_ROOT.VIEW_FILE.self::$thme.HZHIHU_DIREND.$config->theme.
        				HZHIHU_DIREND.'template'.HZHIHU_DIREND.self::$do.HZHIHU_DIREND.$tpl);
        	}
        }else{
       	 include(HZHIHU_ROOT.VIEW_FILE.self::$thme.HZHIHU_DIREND.$config->theme.HZHIHU_DIREND.'template'.HZHIHU_DIREND.$tpl);
        }
        
        self::$html=ob_get_contents();
        ob_end_clean();       	
	}
	
	#####輸出顯示層
	static 	public function render($cache=''){
		if(empty(self::$html))
		{
			$url=R::exurl();
	    	$tpl=$url[0].'_'.$url[1].'.php';
	    	self::tpl($tpl);		
		}
		if(empty($cache)){
			if(isset(self::$html) && !empty(self::$html)){
				echo self::$html;
			}
		}else{
			echo $cache;
		}
	}
	
	######返回緩叢
	static public function readerCache(){
		if(isset(self::$html) && !empty(self::$html)){
			return self::$html;
		}		else{
			return ;
		}
	}
	
	######設置模版ID
	static public function templateid($tid,$key='templateid'){
		view::set($key,$tid);
	}
	
	static public function templatechildren($array){		
		static $children=array();
		if(!empty($array)){
			foreach ($array as $ak=>$av){
				if(is_file(VIEW_FILE.self::$thme.HZHIHU_DIREND.$config->theme.HZHIHU_DIREND.'template'.HZHIHU_DIREND.$av)){
					$keyname=basename($av,'.php');
					require_once(VIEW_FILE.self::$thme.HZHIHU_DIREND.$config->theme.HZHIHU_DIREND.'template'.HZHIHU_DIREND.$av);
					if(!isset($children[$keyname])){
						$children[$keyname]=ob_get_contents();       					
					}
					view::set($keyname,$children[$keyname]);
				}
			}
		}
		ob_end_clean();  
	}
	
	####檢查是否有模版文件
	static private function swiTpl($tpl){
		 $config=self::$config;
		 $language=getlan();
		if(!file_exists(HZHIHU_ROOT.VIEW_FILE.self::$thme.HZHIHU_DIREND.$config->theme.HZHIHU_DIREND.'template'.HZHIHU_DIREND.$tpl)){
			if(!file_exists(HZHIHU_ROOT.VIEW_FILE.self::$thme.HZHIHU_DIREND.$config->theme.HZHIHU_DIREND.'template'.
							HZHIHU_DIREND.self::$do.HZHIHU_DIREND.$tpl)){
		   	 exit($language['public']['nofile'].$tpl);
			}
		}
		return $tpl;
	}
	

	
}